﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

namespace BulletOnRails4NET
{
    public static class StringUtils
    {
        public static bool IsBlank(this string source)
        {
            return (source == null ||
                        source.ToString().Trim().Length == 0);
        }
        //Parts taken from http://pragmaticprose.com/PermaLink,guid,a0983813-66b9-49b5-8f3d-b9469333d476.aspx
        public static string HumanizeString(this string source)
        {
            StringBuilder sb = new StringBuilder();

            char last = char.MinValue; char work_c;
            bool cap_nxt_char = true;
            foreach (char c in source)
            {
                work_c = c;
                if (cap_nxt_char)
                {
                    work_c = char.ToUpper(work_c);
                    cap_nxt_char = false;
                }
                if (c == '_')
                {
                    sb.Append(' ');
                    cap_nxt_char = true;
                }
                else
                {

                    if (char.IsLower(last) &&
                    char.IsUpper(work_c))
                    { sb.Append(' '); }
                    sb.Append(work_c);
                    last = work_c;
                }
            }
            return sb.ToString();
        }
    }
}
